package com.tledu.stu_manager.dao;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.sql.Condition;
import com.tledu.stu_manager.exception.CustomException;
import com.tledu.stu_manager.model.Role;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Antg
 * @date 2021/8/120:30
 */
public class RoleDao {
    /**
     * 查询所有
     *
     * @author Antg
     * @date 2021/8/1 20:30
     */
    public List<Role> findAll() throws CustomException {
        try {
            String sql = "select * from role where del_state = 0";
            List<Entity> roleList = Db.use().query(sql);
            List<Role> resultList = new ArrayList<Role>();
            for (Entity entity : roleList) {
                Role role = new Role();
                role.setId(entity.getInt("id"));
                role.setRoleName(entity.getStr("role_name"));
                role.setCreateTime(entity.getDate("create_time"));
                resultList.add(role);
            }
            return resultList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 根据id查询
     *
     * @author Antg
     * @date 2021/8/1 20:40
     */
    public Role findById(Integer id) throws CustomException {
        String sql = "select * from role where id = ? and  del_state = 0";
        try {
            List<Entity> queryList = Db.use().query(sql, id);
            Entity entity = null;
            if (queryList != null || queryList.size() != 0) {
                entity = queryList.get(0);
            }
            Role role = new Role();
            role.setId(entity.getInt("id"));
            role.setRoleName(entity.getStr("role_name"));
            role.setCreateTime(entity.getDate("create_time"));
            return role;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 根据role_name模糊查询
     *
     * @author Antg
     * @date 2021/8/1 20:43
     */
    public List<Role> findByRoleName(String roleName) throws CustomException {
        try {
            List<Entity> list = Db.use().findBy("role", new Condition("del_state", "=", 0), new Condition("role_name", roleName, Condition.LikeType.Contains));
            List<Role> resultList = new ArrayList<Role>();
            for (Entity entity : list) {
                Role role = new Role();
                role.setId(entity.getInt("id"));
                role.setRoleName(entity.getStr("role_name"));
                role.setCreateTime(entity.getDate("create_time"));
                resultList.add(role);
            }
            return resultList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 添加
     *
     * @author Antg
     * @date 2021/8/1 20:48
     */
    public void addRole(Role role) throws CustomException {
        String sql = "insert into role(role_name) value (?)";
        try {
            Db.use().execute(sql, role.getRoleName());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 修改
     *
     * @author Antg
     * @date 2021/8/1 20:48
     */
    public void editRole(Role role) throws CustomException {
        String sql = "update role set role_name = ? where id = ?";
        try {
            Db.use().execute(sql, role.getRoleName(), role.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 删除
     *
     * @author Antg
     * @date 2021/8/1 20:48
     */
    public void delRole(Integer id) throws CustomException {
        String sql = "update role set del_state = 1 where id = ?";
        try {
            Db.use().execute(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }

    /**
     * 批量删除
     *
     * @author Antg
     * @date 2021/8/1 20:48
     */
    public void batchDelRole(Integer[] idList) throws CustomException {
        String paramStr = "";
        for (int i = 0; i < idList.length; i++) {
            if (i == idList.length - 1) {
                paramStr += "?";
                break;
            }
            paramStr += "?,";
        }
        String sql = "update role set del_state = 1 where id in (" + paramStr + ")";
        try {
            Db.use().execute(sql, idList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new CustomException("数据库连接异常");
        }
    }
}
